package org.jeecg.modules.lab.entrust.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.entity.LabWtemporarily;
import org.jeecg.modules.lab.entrust.service.ILabEntrustItemsService;
import org.jeecg.modules.lab.entrust.service.ILabWtemporarilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 临时任务单
 * @Author: jeecg-boot
 * @Date:   2023-11-30
 * @Version: V1.0
 */
@Api(tags="临时任务单")
@RestController
@RequestMapping("/entrust/labWtemporarily")
@Slf4j
public class LabWtemporarilyController extends JeecgController<LabWtemporarily, ILabWtemporarilyService> {
	@Autowired
	private ILabWtemporarilyService labWtemporarilyService;
	@Autowired
	private ILabCronJobService labCronJobService;
	@Autowired
	private ILabEntrustItemsService labEntrustItemsService;
	/**
	 * 分页列表查询
	 *
	 * @param labWtemporarily
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "临时任务单-分页列表查询")
	@ApiOperation(value="临时任务单-分页列表查询", notes="临时任务单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LabWtemporarily>> queryPageList(LabWtemporarily labWtemporarily,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LabWtemporarily> queryWrapper = QueryGenerator.initQueryWrapper(labWtemporarily, req.getParameterMap());
		//QueryWrapper<LabWtemporarily> queryWrapper = new QueryWrapper<>();
		if(labWtemporarily.getStartDate() != null){
			queryWrapper.eq("start_date",labWtemporarily.getStartDate());
		}
		if(labWtemporarily.getEndDate() != null){
			queryWrapper.eq("end_date",labWtemporarily.getEndDate());
		}
		Page<LabWtemporarily> page = new Page<LabWtemporarily>(pageNo, pageSize);
		IPage<LabWtemporarily> pageList = labWtemporarilyService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	/**
	 * 分页列表查询
	 *
	 * @param labWtemporarily
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "临时任务单-分页列表查询")
	@ApiOperation(value="临时任务单-分页列表查询", notes="临时任务单-分页列表查询")
	@GetMapping(value = "/lists2")
	public Result<IPage<LabEntrustItems>> queryPageLists2(LabWtemporarily labWtemporarily,
														  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														  HttpServletRequest req) {
		QueryWrapper<LabEntrustItems> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("entrust_id",labWtemporarily.getId());
		Page<LabEntrustItems> page = new Page<LabEntrustItems>(pageNo, pageSize);
		IPage<LabEntrustItems> pageList = labEntrustItemsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	/**
	 *   添加
	 *
	 * @param labWtemporarily
	 * @return
	 */
	@AutoLog(value = "临时任务单-添加")
	@ApiOperation(value="临时任务单-添加", notes="临时任务单-添加")
//	@RequiresPermissions("Wtemporarily:lab_wtemporarily:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LabWtemporarily labWtemporarily) {
		labWtemporarilyService.save(labWtemporarily);
		if (MyEnum.sync) {
			long time = new Date().getTime();
			LabCronJob job=new LabCronJob("lab_wtemporarily", "0", time, 1);
			labCronJobService.save(job);
		}
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param labWtemporarily
	 * @return
	 */
	@AutoLog(value = "临时任务单-编辑")
	@ApiOperation(value="临时任务单-编辑", notes="临时任务单-编辑")
//	@RequiresPermissions("Wtemporarily:lab_wtemporarily:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LabWtemporarily labWtemporarily) {
		labWtemporarily.setSynFlag("1");
		labWtemporarilyService.updateById(labWtemporarily);
		if (MyEnum.sync) {
			long time = new Date().getTime();
			LabCronJob job=new LabCronJob("lab_wtemporarily", "1", time, 1);
			labCronJobService.save(job);
		}
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "临时任务单-通过id删除")
	@ApiOperation(value="临时任务单-通过id删除", notes="临时任务单-通过id删除")
//	@RequiresPermissions("Wtemporarily:lab_wtemporarily:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		labWtemporarilyService.removeById(id);
		if (MyEnum.sync) {
			long time = new Date().getTime();
			String condition="id='"+id+"'";
			LabCronJob job=new LabCronJob("lab_wtemporarily", "2",condition, time, 1);
			labCronJobService.save(job);
		}
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "临时任务单-批量删除")
	@ApiOperation(value="临时任务单-批量删除", notes="临时任务单-批量删除")
//	@RequiresPermissions("Wtemporarily:lab_wtemporarily:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.labWtemporarilyService.removeByIds(Arrays.asList(ids.split(",")));

		if (MyEnum.sync) {
			long time = new Date().getTime();
			String[] idList = ids.split(",");
			labCronJobService.saveCronBatch("lab_wtemporarily", "2", time, idList);
		}
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "临时任务单-通过id查询")
	@ApiOperation(value="临时任务单-通过id查询", notes="临时任务单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LabWtemporarily> queryById(@RequestParam(name="id",required=true) String id) {
		LabWtemporarily labWtemporarily = labWtemporarilyService.getById(id);
		if(labWtemporarily==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(labWtemporarily);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param labWtemporarily
    */
//    @RequiresPermissions("Wtemporarily:lab_wtemporarily:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabWtemporarily labWtemporarily) {
        return super.exportXls(request, labWtemporarily, LabWtemporarily.class, "临时任务单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("Wtemporarily:lab_wtemporarily:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabWtemporarily.class);
    }

}
